<?php
namespace app\admin\controller;
use app\admin\model\Gcategory;
use app\admin\model\goods;
use app\admin\model\GoodsProp;
use app\admin\model\GoodsPropValue;
use app\BaseController;
use think\facade\Db;
use think\facade\View;
use think\facade\Request;

class Cate extends BaseController
{
    public function cate()
    {
        $cate = \app\admin\model\Gcategory::getCateList();
        View::assign('cate',$cate);
        if (Request::isPost()) {
            $post = Request::post('', '', 'trim,htmlspecialchars,strip_tags');
            try{
                validate(\app\admin\validate\cate::class)->check($post);
            }catch (\think\exception\ValidateException $e){
                $this->error($e->getMessage());
            }
            $cateadd = new \app\admin\model\Gcategory();
            $db = $cateadd->add($post);
            if ($db){
                $this->success("成功".$db);
            }else{
                $this->error("失败！原因".$cateadd->getError());
            }
            if ($db) {
                echo '成功' . $db;
            } else {
                echo '失败！' . $cateadd->getError();
            }
        }
        $id = Request::get('id', '0', 'trim,htmlspecialchars,strip_tags');
        $list = \app\admin\model\Gcategory::getcate($id);


        $empty = "<tr><td align='conter' colspan='7'>没有数据</td></rt>";
        View::assign('empty', $empty);
        return View::fetch('cate/cate', ['list' => $list]);

    }
    public function cateedit(){
        if (Request::isPost()){
            $post = Request::post('','','trim,htmlspecialchars,strip_tags');
            try{
                validate(\app\admin\validate\cate::class)->scene('edit')->check($post);
            }catch (\think\exception\ValidateException $e){
                $this->error($e->getMessage());
            }
            $cateObj = new Gcategory();
            $result = $cateObj->upt($post);
            if ($result){
                $this->success('修改成功',url('cate'));
            }else{
                $this->error('修改失败');
            }

        }else{
            $cate = Gcategory::getGcategory();
            View::assign('cate',$cate);

            $id = Request::get('id','','trim,htmlspecialchars,strip_tags');
            $cateObj = Gcategory::find($id);
            if (empty($cateObj)){
                $this->error('参数错误',url('cate'));
            }
            return View::fetch('',$cateObj->toArray());
        }
    }
    public function del(){
        $id = Request::get('id','','trim,htmlspecialchars,strip_tags');
        $get = Request::get('','','trim,htmlspecialchars,strip_tags');
        if (isset($get[0]) && 1 == $get[0])     unset($get[0]);
        if (isset($get['id']))        unset($get['id']);

        if (empty($id))     $this->error('非法参数','cate');
        try{
            Gcategory::destroy($id);
        }catch (\Exception $e){
            $this->error('删除失败',url('cate',$get));
        }
        $this->success('删除成功',url('cate',$get));
    }
}